package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class AdChannelServiceimpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {


    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {

        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);//缺少参数
        }

        dto.checkParam();//分页校验
        Page<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNoneBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName, dto.getName());
        }

        IPage<AdChannel> pageRes = this.page(page, queryWrapper);

        PageResponseResult pageResponseResult = new PageResponseResult((int) pageRes.getCurrent(), (int) pageRes.getSize(), (int) pageRes.getTotal());

        pageResponseResult.setData(pageRes.getRecords());

        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdChannel::getIsDefault, 1);
        AdChannel adChannel = this.getOne(queryWrapper);

        if (adChannel != null) {
            adChannel.setIsDefault(false);
            this.updateById(adChannel);
        }
        channel.setIsDefault(true);
        channel.setCreatedTime(new Date());
        boolean save = this.save(channel);


        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {

        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        AdChannel adChannelRes = this.getById(adChannel.getId());

        if (adChannelRes == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        adChannel.setCreatedTime(new Date());
        this.updateById(adChannel);
        return ResponseResult.okResult();

    }


    @Override
    public ResponseResult deleteById(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        AdChannel result = this.getById(id);
        if (result == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        if (result.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.STARUP_STATE);
        }

        this.removeById(id);

        return ResponseResult.okResult();
    }


}
